using System;
using System.Linq;
using YTS.Tools;
using YTS.Test;
using System.Collections.Generic;

namespace Test.ConsoleProgram.Base
{
    public class CaseModelTestTemplate : AbsTestTemplate<CaseModel>
    {
        private ITestFramework testFramework;
        private ITestOutput output;

        public CaseModelTestTemplate(
            ITestFramework testFramework,
            ITestOutput output)
        {
            this.testFramework = testFramework;
            this.output = output;
        }

        public override decimal ExecuteTest(CaseModel item)
        {
            item.NameSign = item.NameSign ?? item.GetType().FullName;
            item.ExeEvent = item.ExeEvent ?? (() => true);

            item.onInit();

            bool isSuccess = false;
            double time_exe = RunHelp.GetRunTime(() =>
            {
                isSuccess = item.ExeEvent();
            });
            if (!isSuccess)
            {
                throw new TestFailException("测试返回结果为错误!", null);
            }

            return (decimal)time_exe;
        }

        public override void ExecuteEnd(CaseModel item)
        {
            item.SonCases = item.SonCases ?? new List<CaseModel>();
            foreach (var son in item.SonCases)
            {
                son.NameSign = $"{item.NameSign} - {son.NameSign}";
                testFramework.OnExecute(this, son, son.NameSign);
            }
        }
    }
}
